package com.recharge.init;


import com.recharge.common.result.ResultDO;
import com.recharge.common.util.Constant;
import com.recharge.common.util.RedisUtil;

import com.recharge.domain.vo.GoodsDO;
import com.recharge.domain.vo.MerchantAccountDO;
import com.recharge.domain.vo.ProviderDO;

import com.recharge.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
public class SpringBootInitialization implements ApplicationRunner {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    private MerchantAccountService merchantAccountService;

    @Autowired
    private ProviderService providerService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private RedisUtil redisUtil;

    @Value("${node.name}")
    private Integer nodeName;


    /**
     * 系统初始化 ->进行缓存预热：
     *              1）预热下游商家账户数据；
     *              2）预热供应商 （阈值，发送数量，最后时间）；
     *              3）预热系统上线商品；
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.debug("初始化系统");
        ResultDO result = merchantAccountService.listall();
        List<MerchantAccountDO> list = (List<MerchantAccountDO>) result.getModel(ResultDO.FIRST_MODEL_KEY);
        if(list != null) {
            for(MerchantAccountDO domain : list) {
                redisUtil.set(Constant.MERCHANT_ACCOUNT + ":" + domain.getId(), domain);
            }
        }

        result = providerService.listAll();
        List<ProviderDO> list2 = (List<ProviderDO>) result.getModel(ResultDO.FIRST_MODEL_KEY);
        if(list2 != null) {
            for(ProviderDO domain : list2) {
                redisUtil.set(Constant.PROVIDER_THRESHOLD + ":" + domain.getId(), domain.getThreshold());
                redisUtil.set(Constant.PROVIDER_CURRENT_SEND_COUNT + ":" + domain.getId(), Integer.valueOf(0));
                redisUtil.set(Constant.PROVIDER_LAST_TIME  + ":" +  domain.getId(), Integer.valueOf(0));
            }
        }

        result = providerService.listCardProviderAll();
        List<ProviderDO> list4 = (List<ProviderDO>) result.getModel(ResultDO.FIRST_MODEL_KEY);
        if(list4 != null) {
            for(ProviderDO domain : list2) {
                redisUtil.set(Constant.PROVIDER_CARD + ":" + domain.getId(), domain.getCallback());
            }
        }
       result = goodsService.listall();
        List<GoodsDO> list3 = (List<GoodsDO>) result.getModel(ResultDO.FIRST_MODEL_KEY);
        logger.debug("加载所有上线的产品");
        if(list3 != null) {
            for(GoodsDO domain : list3) {
                redisUtil.set(Constant.GOODS_INFO + ":" + domain.getGoodsNo(), domain);
            }
        }
        logger.debug("加载所有上线的产品完成");

    }

}
